angular.module('app').directive('appPositionType', [function () {
    return {
        restrict: 'A',
        replace: true,
        transclude: true,
        templateUrl: 'view/template/position-type.html',
        scope: {
            btnlist: '='


        },
        link: function ($scope) {
            $scope.$watch('btnlist', function(newV, oldV, scope){
                if(newV){
                    var count = $scope.btnlist.length;
                    $scope.currentIndex = 0;
                    while ($scope.currentIndex != count && (!$scope.btnlist[$scope.currentIndex].positionList || $scope.btnlist[$scope.currentIndex].positionList.length < 1)) {
                        $scope.currentIndex++;
                    }
                    if ($scope.currentIndex == count && (!$scope.btnlist[$scope.currentIndex].positionList || $scope.btnlist[$scope.currentIndex].positionList.length < 1)) {
                        $scope.currentIndex = -1;
                    }
                }
            });
            $scope.btnclickfn = function (index) {
                if (index == $scope.currentIndex) {
                    return;
                }
                if ($scope.btnlist[index].positionList && $scope.btnlist[index].positionList.length > 0) {
                    $scope.currentIndex = index;
                }

            }
        }

    };
}]);